package com.njtoyo.taxi.admin.logic.business;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.njtoyo.taxi.admin.logic.business.ride.CancellationTransactionLogLogic;
import com.njtoyo.taxi.admin.logic.business.simple_call.RideTransactionLogLogic;
import com.njtoyo.taxi.admin.mapper.master.entity.CancellationTransactionLog;
import com.njtoyo.taxi.admin.mapper.master.entity.Recharge;
import com.njtoyo.taxi.admin.mapper.master.entity.RideTransactionLog;
import com.njtoyo.taxi.admin.mapper.master.mapper.RechargeMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.RefundMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.RideTransactionLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class RechargeLogic {

    @Autowired
    private RefundMapper refundMapper;

    @Autowired
    private RideTransactionLogMapper rideTransactionLogMapper;

    @Autowired
    private RechargeMapper rechargeMapper;

    @Autowired
    private RideTransactionLogLogic rideTransactionLogLogic;

    @Autowired
    private CancellationTransactionLogLogic cancellationTransactionLogLogic;

    // 根据 orderId 修改refunded字段为true
    public boolean updateRefundedByOrderId(Long orderId) {
        RideTransactionLog log = rideTransactionLogLogic.getLogByRideOrderId(orderId);

        if (Objects.nonNull(log)) {
            return new LambdaUpdateChainWrapper<>(rechargeMapper)
                    .eq(Recharge::getId, log.getRechargeId())
                    .set(Recharge::getRefunded, true)
                    .update();
        }

        return false;
    }

    // 根据 cancellationOrderId 修改refunded字段为true
    public boolean updateRefundedByCancellationOrderId(Long cancellationOrderId) {
        CancellationTransactionLog log = cancellationTransactionLogLogic.getLogByCancellationOrderId(cancellationOrderId);

        if (Objects.nonNull(log)) {
            return new LambdaUpdateChainWrapper<>(rechargeMapper)
                    .eq(Recharge::getId, log.getRechargeId())
                    .set(Recharge::getRefunded, true)
                    .update();
        }

        return false;
    }


}
